<?php

namespace common\models\base;

use Yii;

/**
 * This is the model class for table "{{%user}}".
 *
 * @property int $id
 * @property string $username
 * @property string $auth_key
 * @property string $password_hash
 * @property string $password_reset_token
 * @property string $email
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property string $token api认证令牌
 * @property string $sign_key 签名钥匙
 * @property string $phone 手机号
 * @property int $user_type 用户类型
 * @property string $company 公司
 * @property string $tp_pwd
 *
 * @property ApiRoleUser[] $apiRoleUsers
 * @property ApiRole[] $apiRoles
 * @property CarGpsOrder[] $carGpsOrders
 * @property CarGpsOrder[] $carGpsOrders0
 * @property UserGuaranteeCompanyDotSiteBind[] $userGuaranteeCompanyDotSiteBinds
 * @property GuaranteeCompanyDotSite[] $guaranteeCompanyDotSites
 */
class User extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%user}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['username', 'auth_key', 'password_hash', 'email', 'created_at', 'updated_at', 'user_type'], 'required'],
            [['status', 'created_at', 'updated_at', 'user_type'], 'integer'],
            [['tp_pwd'], 'string'],
            [['username', 'password_hash', 'password_reset_token', 'email'], 'string', 'max' => 255],
            [['auth_key', 'token', 'sign_key'], 'string', 'max' => 32],
            [['phone'], 'string', 'max' => 11],
            [['company'], 'string', 'max' => 50],
            [['username'], 'unique'],
            [['email'], 'unique'],
            [['phone'], 'unique'],
            [['password_reset_token'], 'unique'],
            [['token'], 'unique'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'username' => Yii::t('app', 'Username'),
            'auth_key' => Yii::t('app', 'Auth Key'),
            'password_hash' => Yii::t('app', 'Password Hash'),
            'password_reset_token' => Yii::t('app', 'Password Reset Token'),
            'email' => Yii::t('app', 'Email'),
            'status' => Yii::t('app', 'Status'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
            'token' => Yii::t('app', 'api认证令牌'),
            'sign_key' => Yii::t('app', '签名钥匙'),
            'phone' => Yii::t('app', '手机号'),
            'user_type' => Yii::t('app', '用户类型'),
            'company' => Yii::t('app', '公司'),
            'tp_pwd' => Yii::t('app', 'Tp Pwd'),
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoleUsers()
    {
        return $this->hasMany(ApiRoleUser::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getApiRoles()
    {
        return $this->hasMany(ApiRole::className(), ['id' => 'api_role_id'])->viaTable('{{%api_role_user}}', ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarGpsOrders()
    {
        return $this->hasMany(CarGpsOrder::className(), ['created_by' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCarGpsOrders0()
    {
        return $this->hasMany(CarGpsOrder::className(), ['updated_by' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserGuaranteeCompanyDotSiteBinds()
    {
        return $this->hasMany(UserGuaranteeCompanyDotSiteBind::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGuaranteeCompanyDotSites()
    {
        return $this->hasMany(GuaranteeCompanyDotSite::className(), ['id' => 'guarantee_company_dot_site_id'])->viaTable('{{%user_guarantee_company_dot_site_bind}}', ['user_id' => 'id']);
    }
}
